<?php

namespace App\Talk;


use Easemob\Attachment;
use Easemob\Auth;
use Easemob\Contact;
use Easemob\Message;
use Easemob\User;
use Easemob\UserMetadata;

class Easemob
{

    public $auth;

    public $user;


    public $message;


    public $attachment;


    public  $userMetadata;

    /**
     * @throws \Exception
     */
    public function __construct()
    {
        $appKey = env('HX_APP_KEY');
        $clientId = env('HX_CLIENT_ID');
        $clientSecret = env('HX_CLIENT_SECRET');
        $dev = env('APP_ENV');





        $this->auth = new Auth($appKey, $clientId, $clientSecret);

        if($dev != 'dev'){
            $this->auth->setApiUri('https://a41.easemob.com');
        }
        $this->auth->setApiUri('https://a41.easemob.com');

        $this->user = new User($this->auth);
        $this->message = new Message($this->auth);
        $this->attachment = new Attachment($this->auth);
        $this->userMetadata = new UserMetadata($this->auth);


    }


    /**
     * 环信创建用户
     * @param $username
     * @param $password
     * @param $nickname
     * @return bool
     */
    public function createUser($username, $password, $nickname): bool
    {
        $data = [
            'username' => $username,
            'password' => $password,
            'nickname' => $nickname,
        ];

        $result = $this->user->create($data);


        if (isExistence($result, ['uuid'])) {
            return true;
        }
        if (isExistence($result, ['code', 'error'])) {
            if ($result['code'] == 400 && $result['error'] == "duplicate_unique_property_exists") {
                return true;
            }
        }
        return false;
    }

    public function setMetadataToUser($username,$media){

        return $this->userMetadata->setMetadataToUser($username,$media);

    }


    public function getMetadataToUser($username){
        return $this->userMetadata->getMetadataFromUser($username);
    }





    /**
     * 获取登录的信息
     * @param $username
     * @param $password
     * @return array|false
     */
    public function getToken($username, $password)
    {
        $result = $this->auth->getUserToken($username, $password, 3600*60);

        if (isExistence($result, ['access_token', 'expires_in'])) {
            $result['username'] = $username;
            return $result;
        } else {
            return false;
        }
    }


    /**
     * rest接口 发送文本信息
     * @param array $user 发送的用户组 单个也必须数组 ex ['user1']
     * @param string $msg 发送文本信息的内容
     * @param string $from 发送人
     * @return bool
     */
    public function sendText(array $user, string $msg, string $from = 'admin'): bool
    {
        $result = $this->message->text('users', $user, ['msg' => $msg], $from);
        if (isExistence($result, ['code'])) {
            return false;
        }
        return true;
    }


    /**
     * rest接口 发送图片信息
     * @param array $user 发送的用户组 单个也必须数组 ex ['user1']
     * @param string $uuid 上传图片结果body中的uuid
     * @param string $from 发送人
     * @return bool
     */
    public function sendImg(array $user, string $uuid, string $from = 'admin'): bool
    {

        $result = $this->message->image('users', $user, ['uuid' => $uuid], $from);
        if (isExistence($result, ['code'])) {
            return false;
        }
        return true;
    }


    /**
     * rest接口 上传文件
     * @param string $fileName 文件路径 仅限临时路径
     * @return array
     */
    public function uploadFile(string $fileName): array
    {
        return $this->attachment->uploadFile($fileName);
    }


    /**
     * 查看用户是否在线
     * @param string $user
     * @return bool
     */
    public function isUserOnline(string $user): bool
    {
        return $this->user->isUserOnline($user);
    }



}
